import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/ui/food/batch_cancel_food_page.dart';
import 'package:ls_work/ui/food/single_order_select_place_page.dart';

import '../../net/post_request.dart';
import '../../net/response/food/order_model_list_data.dart';
import '../../viewmodel/food/old_order_food_detail_viewm_model.dart';
import '../select_employee_page.dart';

class OldOrderFoodDetailPage extends BaseStatefulMvvmWidget {
  final String oldId;
  final SelectFoodHuman? oldInfo;

  OldOrderFoodDetailPage(this.oldId, {this.oldInfo});

  @override
  State<StatefulWidget> createState() => _OldOrderFoodDetailState();
}

class _OldOrderFoodDetailState extends BaseMvvmListState<
    OldOrderFoodDetailViewModel, OldOrderFoodDetailPage> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("点餐详情"),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    var date = item.date?.replaceAll(" ", "\n");
    return Row(
      children: [
        CommonText('${date}',
                height: 1.3, textAlign: TextAlign.center, textSize: 15)
            .setWeight(4),
        (item.breakfast != null
                ? Icon(
                    Icons.check_circle,
                    color: Colors.green,
                  )
                : CommonText('点餐', textAlign: TextAlign.center, textSize: 15))
            .onTap(() {
          toOrderPage(mealId: item.breakfast);
        }).setWeight(3),
        (item.lunch != null
                ? Icon(
                    Icons.check_circle,
                    color: Colors.green,
                  )
                : CommonText('点餐', textAlign: TextAlign.center, textSize: 15))
            .onTap(() {
          toOrderPage(mealId: item.lunch);
        }).setWeight(3),
        (item.dinner != null
                ? Icon(
                    Icons.check_circle,
                    color: Colors.green,
                  )
                : CommonText('点餐', textAlign: TextAlign.center, textSize: 15))
            .onTap(() {
          toOrderPage(mealId: item.dinner);
        }).setWeight(3),
        (item.canCancel
                ? CommonText('取消', textAlign: TextAlign.center, textSize: 15)
                    .onTap(() {
                    showCancelFoodSheet(context,
                        breakfastId: item.breakfast,
                        lunchId: item.lunch,
                        dinnerId: item.dinner,
                        date: item.date);
                  })
                : Container())
            .setWeight(3),
      ],
    ).addToContainer(
        color: index % 2 == 0 ? Colors.white : Colors.grey[100],
        padding: EdgeInsets.symmetric(vertical: 6));
  }

  @override
  Widget buildLoadingContentView() {
    var old = widget.oldInfo;
    return Column(
      children: [
        if (old != null)
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ImageLoad(
                "${HttpApi.imageHead}${old.headImage}",
                placeholder: 'assets/none.png',
                errorImage: 'assets/none.png',
                width: 70,
                height: 70,
                fit: BoxFit.cover,
              ),
              Container(
                width: 10,
              ),
              Column(
                children: [
                  Container(
                    height: 10,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      CommonText('姓名：${old.name}', textSize: 14).setWeight(5),
                      CommonText('年龄：${old.age==null?'——':'${old.age}岁'}', textSize: 14).setWeight(5),
                      CommonText('性别：${old.sex??"——"}', textSize: 14)
                          .setWeight(4),
                    ],
                  ),
                  Container(
                    height: 20,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      CommonText('床位号：${old.bedNo??'——'}', textSize: 14).setWeight(5),
                      CommonText(
                              '餐别：${viewModel.packageInfo.isNullOrEmpty() ? '——' : viewModel.packageInfo}',
                              textSize: 14)
                          .setWeight(5),
                      CommonText('', textSize: 14).setWeight(4),
                    ],
                  )
                ],
              ).setWeight(1)
            ],
          ).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10)),
        Row(
          children: [
            CommonText('日期', textAlign: TextAlign.center, textSize: 15)
                .setWeight(4),
            CommonText('早餐', textAlign: TextAlign.center, textSize: 15)
                .setWeight(3),
            CommonText('午餐', textAlign: TextAlign.center, textSize: 15)
                .setWeight(3),
            CommonText('晚餐', textAlign: TextAlign.center, textSize: 15)
                .setWeight(3),
            CommonText('').setWeight(3),
          ],
        ).addToContainer(
            color: Colors.grey[100],
            padding: EdgeInsets.symmetric(vertical: 6)),
        super.buildLoadingContentView()!.setWeight(1)
      ],
    );
  }

  @override
  OldOrderFoodDetailViewModel createViewModel() {
    return OldOrderFoodDetailViewModel(context, widget.oldId);
  }

  Future<void> toOrderPage({ int? mealId}) async {
    var data = await getOrderType();
    Go().push(SingleOrderSelectPlacePage(
      data?.orderType.toString()??"",
      data?.id.toString()??"",
      oldId: widget.oldId,
      mealId: mealId,
    ));
  }

  Future<OrderModelListData?> getOrderType() async {
    OrderModelListData? item;
    showLoadingDialog("");
    try {
      var data = await PostRequest.getOrderModel(1, 20);
      if ('0' == data['code'].toString()) {
        var list = ((data['data']['rows'] as List?)??[])
            .map((e) => OrderModelListData.fromJson(e))
            .toList();
        for (var type in list) {
          if (type.orderType == 2) {
            item = type;
            break;
          }
        }
      } else {
        item = null;
      }
      hideDialog();
    } catch (e) {
      hideDialog();
      item = null;
    }
    return item;
  }
}
